問題描述
如何通過單擊歌曲列表來播放歌曲 (How to play the song by clicking the lists of song)
i'm new in Android Programming..I want to build MediaPlayer application.
Actually i'm already built a simple MediaPlayer application before..My previous app is just have one button play,one button pause,and one button stop..when clicking the play button the song will play.
And my new application is want to do list of song using ListLayout and a few buttons like play,pause,and stop. what i want is when i click the first song in the lists of song,the first song will play..
I already some research in google but dont found the simple answer that i can understand..I hope you guys can help me..Thank you a lots..=)
This is my UPDATED code..I dont know how to call the lists of song..i store my song1,song2,song3 in the raw file..but for the play the song,i just only can play one song only..
package com.example.test7;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
MediaPlayer mp;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = MediaPlayer.create(this,R.raw.song1);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.listView1);
String[] values = new String[] {"song1","song2","song3","song4","song5",
"song6","song7","song8","song9","song10"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener () {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView list2 = (ListView) findViewById(R.id.listView1);
String item = (String) list2.getAdapter().getItem(position);
Toast.makeText(MainActivity.this, item + " selected", 5).show();
mp.start();
}
});
ImageButton btnPlay = (ImageButton) this.findViewById(R.id.imageButton1);
ImageButton btnPause = (ImageButton) this.findViewById(R.id.imageButton2);
ImageButton btnStop = (ImageButton) this.findViewById(R.id.imageButton3);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(MainActivity.this, R.raw.song1);
}
if (!mp.isPlaying()) {
mp.start();
}
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null)
{
mp.pause();
position = mp.getCurrentPosition();
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null)
{
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
}
});
}
}
參考解法
方法 1:
mp.reset(); need to implement in the code.
方法 2:
u can implement it by using the final int value of the song which will be present in r.java Note: check for the final int value of all song in R.java file.
final int[] mid = new int[] {0x7f040000,0x7f040001,0x7f040002,0x7f040003,0x7f040004,0x7f040005};
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
s=MediaPlayer.create(SimpleListViewActivity.this, mid[position]);
s.start();
}
});
(by paehmods、william、DINESH kumar)